package cn.k23.controller;

import cn.k23.mapper.UserLogMapper;
import cn.k23.mapper.UserMapper;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: k23
 * @Date: 2022-03-30 11:15
 * @Version: 1.0
 */
@Controller
public class UserController {
    @Autowired
    private UserMapper um;
    @Autowired
    private UserLogMapper ul;

    //验证码
    @GetMapping("/checks")
    public void check(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        GifCaptcha c = new GifCaptcha(95, 36, 4);
        CaptchaUtil.out(c, req, resp);
    }

    //检查登录
    @PostMapping("/checklogin")
    @ResponseBody
    public Map<String, Object> checklogin(@RequestBody Map<String, Object> v, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 300);
        //判断验证码
        HttpSession session = req.getSession();
        if (v.get("cc").toString().equalsIgnoreCase(session.getAttribute("captcha").toString())) {
            if (um.login(v.get("account").toString(), v.get("passwd").toString()) == 1) {
                map.put("code", 200);
                map.put("login", true);
                map.put("msg", "登录成功");

                session.setAttribute("user", v.get("account"));

                //设置session
                Map<String, String> vvv = new HashMap<>();
                vvv.put("account", v.get("account").toString());
                vvv.put("ip", req.getRemoteAddr());
                vvv.put("info", "登录成功");
                ul.insert(vvv);

            } else {
                map.put("login", false);
                map.put("msg", "登录失败，请检查你的账号和密码");
                Map<String, String> vvv = new HashMap<>();
                vvv.put("account", v.get("account").toString());
                vvv.put("ip", req.getRemoteAddr());
                vvv.put("info", "登录失败，请检查你的账号和密码");
                ul.insert(vvv);
            }
        } else {
            map.put("login", false);
            map.put("msg", "登录失败，验证码错误");
        }
        return map;
    }

    //退出登录
    @GetMapping("/logout")
    public String logout(HttpSession session, HttpServletRequest req) {
        //设置session
        if (session.getAttribute("user") == null) {
        } else {
            //记录操作日志
            Map<String, String> vvv = new HashMap<>();
            vvv.put("account", session.getAttribute("user").toString());
            vvv.put("ip", req.getRemoteAddr());
            vvv.put("info", "安全退出");
            ul.insert(vvv);
            session.removeAttribute("user");
        }
        return "redirect:/login";
    }

    //修改密码
    @PostMapping("/uupass")
    @ResponseBody
    public Map<String, Object> updatePassword(@RequestBody Map<String, Object> v, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 300);
        //原密码对不对
        if(v.get("passwd").toString().equals(um.queryPassword(v.get("account").toString()))){
            //修改密码
            um.updatePassword(v.get("account").toString(),v.get("p1").toString());
            //记录操作日志
            Map<String, String> vvv = new HashMap<>();
            vvv.put("account", v.get("account").toString());
            vvv.put("ip", req.getRemoteAddr());
            vvv.put("info", "成功修改密码");
            ul.insert(vvv);
            map.put("code",200);
            map.put("flag",true);
            map.put("msg","密码修改成功，请牢记你的密码");
        }else{
            map.put("flag",false);
            map.put("msg","密码修改失败，原密码不正确");
        }
        return map;
    }

    //基本资料
    @GetMapping("/usettings")
    public String usettings(Model m, HttpSession session) {
        m.addAttribute("u",um.queryByAcoount(session.getAttribute("user").toString()));
        return "usettings";
    }

    @GetMapping("/upasswd")
    public String upasswd(){
        return "upasswd";
    }

    @GetMapping("/tlive")
    public String tlive() {
        return "tlive";
    }

    @GetMapping("/guestbook")
    public String guestbook() {
        return "guestbook";
    }

    @GetMapping("/score")
    public String score() {
        return "score";
    }


}
